﻿using DL.AnimatorEvents;
using DL.Characters.Player.FSM.Data;
using DL.Characters.Player.FSM.StateMachine;
using DL.Combat;
using DL.Combat.Base;
using DL.Combat.Base.Detection;
using DL.Health;
using DL.Movement;
using DL.Tools;
using DL.UI;
using UnityEngine;

namespace DL.Characters.Player
{
    /// <summary>
    /// 角色主脚本
    /// </summary>
    public class Player : Actor
    {
        //组件
        public PlayerMoveController MovementController { get; private set; }
        public WeaponDetectionCast WeaponDetectionCast { get; private set; }
      
        //实例类
        public PlayerBaseAttackCombatController CombotController { get; private set; }
        public PlayerBaseWeaponAttackCombatController WeaponCombotController { get; private set; }
        public PlayerHealthController HealthController { get; private set; }
        
        /// <summary>
        /// 运行时重用数据
        /// </summary>
        [field:SerializeReference]
        public RuntimeReusableData RuntimeReusableData { get; private set; }
        
        //状态机
        public PlayerMoveStateMachine MoveStateMachine { get; private set; }
        public PlayerCombatStateMachine CombatStateMachine { get; private set; }
        
        private DebugPage DebugPage;

        protected override void OnComponentLoad()
        {
            Animator = GetComponent<Animator>();
            AnimationEvent = GetComponent<AnimatorEvent>();
            MovementController = GetComponent<PlayerMoveController>();
            WeaponDetectionCast = GetComponentInChildren<WeaponDetectionCast>();
            CustomMovementController = GetComponent<CustomMovementController>();
            
            Self = transform;
            HealthController = new(this);
        }

        protected override void OnComponentInit()
        {
            RuntimeReusableData = new();

            CustomMovementController.Init(this);
            MoveStateMachine = new(this);
            MovementController.Init(this);

            CombotController = new(this);
            WeaponCombotController = new(this);
            CombatStateMachine = new(this);

            
            // NewTimerManager.Inst.GetTimer(5f, () =>
            // {
            //     DebugPage = (DebugPage)PageMgr.ShowBasePage("DebugPage");
            //     PageBind();
            // });
        }

        void PageBind()
        {
            CombatStateMachine.CurrentState.OnValueChanged.AddListener((call) =>
            {
                DebugPage.currentBattleState = "当前战斗状态:" + call.name;
            });
            
            MoveStateMachine.CurrentState.OnValueChanged.AddListener((call) =>
            {
                DebugPage.currentMoveState = "当前移动状态:" + call.name;
            });
            
            MovementController.CurrentMovementVelocity.OnValueChanged.AddListener(call =>
                DebugPage.movementVelocity = "当前移动速度:" + call.ToString("F2"));
            MovementController.CurrentVerticalVelocity.OnValueChanged.AddListener(call =>
                DebugPage.verticalVelocity = "当前垂直速度:" + call.ToString("F2"));
        }

        protected override void AddInputActionCallBacks()
        {
            HealthController.AddInputActionCallBacks();
        }

        protected override void RemoveInputActionCallBacks()
        {
            HealthController.RemoveInputActionCallBacks();
        }

        private void FixedUpdate()
        {
            MoveStateMachine.FixedUpdate();
            
            CombatStateMachine.FixedUpdate();
        }

        private void Update()
        {
            MoveStateMachine.CheckAnimatorArgs();
            MoveStateMachine.HandleInput();
            MoveStateMachine.Update();

            CombatStateMachine.CheckAnimatorArgs();
            CombatStateMachine.HandleInput();
            CombatStateMachine.Update();
        }

        /// <summary>
        /// 全部使用CC.Move。不去使用根运动
        /// </summary>
        // private void OnAnimatorMove()
        // {
        //     MoveStateMachine.OnAnimationMove();
        //     
        //     CombatStateMachine.OnAnimationMove();
        // }

        public void OnAnimationExitEvent()
        {
            MoveStateMachine.OnAnimationExitEvent();
            
            CombatStateMachine.OnAnimationExitEvent();
        }

        private void OnDrawGizmosSelected()
        {
            // CombotController?.DrawDetectionRang();
            // CombotController?.AttackRangeIndicator();
            
            WeaponCombotController?.DetectionRangDisplay();
        }
    }
}